/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * FormPenjualan.java
 *
 * Created on 11 Nov 11, 21:01:41
 */
package Penjualan;

import MenuUtamaSistem.Login;
import MenuUtamaSistem.MenuUtamaController;
import Pembelian.PembelianController;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author asus
 */
public class FormPenjualan extends javax.swing.JDialog {

    /** Creates new form FormPenjualan */
    public FormPenjualan() {
        initComponents();
        setLocation(400, 180);
        noJualText.setText("MAFYJ-");
        idUser.setText(Login.getLog().getUser());
        PembelianController.showDateAndClock(tanggaljual, jamLabel1);
        try {
            long stok = PenjualanController.getKoneksi().getSisaStok().get(0).getStok();
        } catch (FileNotFoundException ex) {
            Logger.getLogger(FormPenjualan.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(FormPenjualan.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex){
            Logger.getLogger(FormPenjualan.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        formJual = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        jLabel9 = new javax.swing.JLabel();
        jamJualLabel = new javax.swing.JLabel();
        logoLabel = new javax.swing.JLabel();
        backButton = new javax.swing.JButton();
        saveButton = new javax.swing.JButton();
        satutsJual = new javax.swing.JLabel();
        batalButton = new javax.swing.JButton();
        logo = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        jLabel13 = new javax.swing.JLabel();
        noJualText = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel12 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        providerCombo = new javax.swing.JComboBox();
        nominalText = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        hargaText = new javax.swing.JTextField();
        nomorHpText = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jamLabel1 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        idUser = new javax.swing.JLabel();
        tanggaljual = new javax.swing.JLabel();
        editButton = new javax.swing.JButton();
        BG = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setTitle("Form Penjualan - SP3EF");

        jPanel1.setBackground(new java.awt.Color(153, 204, 255));
        jPanel1.setLayout(null);

        formJual.setFont(new java.awt.Font("Tempus Sans ITC", 1, 24));
        formJual.setText("FORM PENJUALAN");
        jPanel1.add(formJual);
        formJual.setBounds(10, 30, 280, 33);
        jPanel1.add(jSeparator1);
        jSeparator1.setBounds(0, 90, 560, 10);
        jPanel1.add(jLabel9);
        jLabel9.setBounds(140, 70, 0, 0);
        jPanel1.add(jamJualLabel);
        jamJualLabel.setBounds(460, 70, 0, 0);

        logoLabel.setText("@MAFY com 2011");
        jPanel1.add(logoLabel);
        logoLabel.setBounds(240, 420, 150, 20);

        backButton.setText("Kembali Ke Menu Utama");
        backButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                backButtonActionPerformed(evt);
            }
        });
        jPanel1.add(backButton);
        backButton.setBounds(30, 380, 150, 23);

        saveButton.setText("SIMPAN");
        saveButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveButtonActionPerformed(evt);
            }
        });
        jPanel1.add(saveButton);
        saveButton.setBounds(440, 380, 90, 23);
        jPanel1.add(satutsJual);
        satutsJual.setBounds(450, 340, 0, 0);

        batalButton.setText("BATAL");
        batalButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                batalButtonActionPerformed(evt);
            }
        });
        jPanel1.add(batalButton);
        batalButton.setBounds(190, 380, 90, 23);

        logo.setBackground(new java.awt.Color(153, 153, 255));
        logo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Penjualan/HP.png"))); // NOI18N
        jPanel1.add(logo);
        logo.setBounds(440, 0, 120, 90);

        jPanel2.setBackground(new java.awt.Color(255, 255, 255));
        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Data Penjualan"));
        jPanel2.setLayout(null);

        jLabel13.setText("Status Penjualan");
        jPanel2.add(jLabel13);
        jLabel13.setBounds(340, 340, 100, 14);

        noJualText.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                noJualTextActionPerformed(evt);
            }
        });
        jPanel2.add(noJualText);
        noJualText.setBounds(140, 30, 150, 20);

        jLabel4.setText("Nomor Penjualan");
        jPanel2.add(jLabel4);
        jLabel4.setBounds(30, 30, 100, 20);

        jLabel5.setText("Nomor Handphone");
        jPanel2.add(jLabel5);
        jLabel5.setBounds(30, 70, 110, 20);

        jLabel12.setText("Nominal");
        jPanel2.add(jLabel12);
        jLabel12.setBounds(30, 150, 70, 20);

        jLabel6.setText("Provider");
        jPanel2.add(jLabel6);
        jLabel6.setBounds(30, 110, 80, 20);

        providerCombo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "-Pilih Provider-", "IM3", "Mentari", "Simpati", "As", "XL", "Axis", "Esia", "Ceria", "Smart", "Flexi", "StarOne" }));
        jPanel2.add(providerCombo);
        providerCombo.setBounds(140, 110, 110, 20);

        nominalText.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                nominalTextFocusLost(evt);
            }
        });
        jPanel2.add(nominalText);
        nominalText.setBounds(140, 150, 150, 20);

        jLabel7.setText("Harga ");
        jPanel2.add(jLabel7);
        jLabel7.setBounds(30, 190, 70, 20);
        jPanel2.add(hargaText);
        hargaText.setBounds(140, 190, 150, 20);
        jPanel2.add(nomorHpText);
        nomorHpText.setBounds(140, 70, 150, 20);

        jPanel1.add(jPanel2);
        jPanel2.setBounds(20, 110, 340, 240);

        jLabel8.setText("Jam");
        jPanel1.add(jLabel8);
        jLabel8.setBounds(400, 140, 50, 20);

        jLabel2.setText("Tanggal");
        jPanel1.add(jLabel2);
        jLabel2.setBounds(400, 110, 60, 20);
        jPanel1.add(jamLabel1);
        jamLabel1.setBounds(460, 140, 90, 20);

        jLabel11.setText("ID User");
        jPanel1.add(jLabel11);
        jLabel11.setBounds(400, 170, 50, 20);
        jPanel1.add(idUser);
        idUser.setBounds(450, 170, 90, 20);
        jPanel1.add(tanggaljual);
        tanggaljual.setBounds(470, 110, 80, 20);

        editButton.setText("EDIT");
        editButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                editButtonActionPerformed(evt);
            }
        });
        jPanel1.add(editButton);
        editButton.setBounds(340, 380, 90, 23);

        BG.setIcon(new javax.swing.ImageIcon(getClass().getResource("/MenuUtamaSistem/BG.jpg"))); // NOI18N
        jPanel1.add(BG);
        BG.setBounds(0, -10, 570, 470);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 565, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 452, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void backButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_backButtonActionPerformed
        // TODO add your handling code here:
        this.setVisible(false);
        Login.getMain().setVisible(true);
    }//GEN-LAST:event_backButtonActionPerformed

    private void saveButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveButtonActionPerformed

        // TODO add your handling code here:
        if (saveButton.getText().equalsIgnoreCase("SIMPAN")) {

            try {
                //                if(noJualText.getText().equalsIgnoreCase("MAFYJ-")||nomorHpText.getText()==""
                //                        ||providerCombo.getSelectedItem().toString().equalsIgnoreCase("-pilih provider-")||
                //                        nominalText.getText().isEmpty()||hargaText.getText().isEmpty())
                //                {
                //                    System.out.println("Benar");
                //                    JOptionPane.showMessageDialog(null, "Field diisi semua ya");
                //                }
                //                else
                //                {
                System.out.println("salah");
                String noPenjualan = noJualText.getText();
                String noHp = nomorHpText.getText();
                String provider = providerCombo.getSelectedItem().toString();
//                int nominal = Integer.parseInt(nominalText.getText());
//                int harga = Integer.parseInt(hargaText.getText());
                String waktu = tanggaljual.getText();
                Penjualan jual = new Penjualan();
                jual.setNoPenjualan(noPenjualan);
                jual.setNoHp(noHp);
                jual.setProvider(provider);
                jual.setNominal(nominalText.getText());
                jual.setHarga(hargaText.getText());
                jual.setIdUser(idUser.getText());
                jual.setWaktu(waktu);

                PenjualanController kontrol;
                kontrol = PenjualanController.getKoneksi();
//                if(Integer.parseInt(sisaStok.getText())){
                int status = kontrol.inputDataPenjualan(jual);
                if (status == 0) {
                    JOptionPane.showMessageDialog(null, "Data penjualan berhasil ditambah");
                    noJualText.setText("MAFYJ-");
                    nomorHpText.setText("");
                    providerCombo.setSelectedIndex(0);
                    nominalText.setText("");
                    hargaText.setText("");
                } else {
                    JOptionPane.showMessageDialog(null, "Data gagal disimpan. Transaksi sudah ada atau jumlah stok tidak mencukupi");
                }
            } catch (SQLException ex) {
                Logger.getLogger(FormPenjualan.class.getName()).log(Level.SEVERE, null, ex);
                JOptionPane.showMessageDialog(null, "Data Gagal disimpan");
            } catch (FileNotFoundException ex) {
                Logger.getLogger(FormPenjualan.class.getName()).log(Level.SEVERE, null, ex);
                JOptionPane.showMessageDialog(null, "Data Gagal disimpan");
            } catch (IOException ex) {
                Logger.getLogger(FormPenjualan.class.getName()).log(Level.SEVERE, null, ex);
                JOptionPane.showMessageDialog(null, "Data Gagal disimpan");
            } catch (Exception ex) {
                JOptionPane.showMessageDialog(null, "ERROR : " + ex.getMessage());
            }
        } else if (saveButton.getText().equalsIgnoreCase("UPDATE")) {

            try {
                //                if(nomorHpText.getText() == null ? "" == null : nomorHpText.getText().equals(""))
                //                {
                //                    JOptionPane.showMessageDialog(null, "Nomor handphone tidak  boleh kosong");
                //                }
                //                if((nomorHpText.getText() == null ? "" == null : nomorHpText.getText().equals(" "))
                //                        ||providerCombo.getSelectedItem().toString().equalsIgnoreCase("-pilih provider-")||
                //                        nominalText.getText().isEmpty()||hargaText.getText().isEmpty())
                //                {
                //                    System.out.println("Benar");
                //                    JOptionPane.showMessageDialog(null, "Field diisi semua ya");
                //                }
                //                else
                //                {
                Penjualan jual = new Penjualan();
                String no = nomorHpText.getText();
                String prov = providerCombo.getSelectedItem().toString();
//                int nominal = Integer.parseInt(nominalText.getText());
//                int harga = Integer.parseInt(hargaText.getText());
                jual.setNoPenjualan(noJualText.getText());
                jual.setNoHp(no);
                jual.setProvider(prov);
                jual.setNominal(nominalText.getText());
                jual.setHarga(hargaText.getText());

                PenjualanController kontrol = PenjualanController.getKoneksi();
                kontrol.updateDataPenjualan(jual);
                JOptionPane.showMessageDialog(null, "Data penjualan berhasil di update");
                saveButton.setText("SIMPAN");
                noJualText.setEditable(true);
                noJualText.setText("MAFYJ-");
                nomorHpText.setText("");
                providerCombo.setSelectedIndex(0);
                nominalText.setText("");
                hargaText.setText("");

            } catch (SQLException ex) {
                Logger.getLogger(FormPenjualan.class.getName()).log(Level.SEVERE, null, ex);
                JOptionPane.showMessageDialog(null, "Data tidak berhasil di update");
            } catch (FileNotFoundException ex) {
                Logger.getLogger(FormPenjualan.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IOException ex) {
                Logger.getLogger(FormPenjualan.class.getName()).log(Level.SEVERE, null, ex);
            } catch (Exception ex) {
                JOptionPane.showMessageDialog(null, "ERORR : " + ex.getMessage());
            }
        }


    }//GEN-LAST:event_saveButtonActionPerformed

    private void batalButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_batalButtonActionPerformed
        // TODO add your handling code here:
        saveButton.setText("SIMPAN");
        noJualText.setText("MAFYJ-");
        nomorHpText.setText("");
        providerCombo.setSelectedIndex(0);
        nominalText.setText("");
        hargaText.setText("");
    }//GEN-LAST:event_batalButtonActionPerformed

    private void editButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_editButtonActionPerformed
        // TODO add your handling code here:
        editPenjualanForm edit = new editPenjualanForm();
        edit.setForm(this);
        edit.setVisible(true);
        saveButton.setText("UPDATE");
    }//GEN-LAST:event_editButtonActionPerformed

    private void noJualTextActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_noJualTextActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_noJualTextActionPerformed

    private void nominalTextFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_nominalTextFocusLost
        // TODO add your handling code here:
        int harga= Integer.parseInt(nominalText.getText())+Penjualan.getHargaPulsa();
        hargaText.setText(""+harga);
        hargaText.setEnabled(false);
    }//GEN-LAST:event_nominalTextFocusLost

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new FormPenjualan().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel BG;
    private javax.swing.JButton backButton;
    private javax.swing.JButton batalButton;
    private javax.swing.JButton editButton;
    private javax.swing.JLabel formJual;
    public javax.swing.JTextField hargaText;
    private javax.swing.JLabel idUser;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JLabel jamJualLabel;
    private javax.swing.JLabel jamLabel1;
    private javax.swing.JLabel logo;
    private javax.swing.JLabel logoLabel;
    public javax.swing.JTextField noJualText;
    public javax.swing.JTextField nominalText;
    public javax.swing.JTextField nomorHpText;
    public javax.swing.JComboBox providerCombo;
    private javax.swing.JLabel satutsJual;
    private javax.swing.JButton saveButton;
    private javax.swing.JLabel tanggaljual;
    // End of variables declaration//GEN-END:variables
}
